package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.qmyx.admin.dao.*;
import com.ytjj.qmyx.admin.model.request.HotProductRequest;
import com.ytjj.qmyx.admin.model.response.HotProductChoose;
import com.ytjj.qmyx.admin.model.response.HotProductResponse;
import com.ytjj.qmyx.admin.service.HotProductService;
import com.ytjj.qmyx.mall.model.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 
 *
 * @Version: 1.0
 */
@Service
public class HotProductServiceImpl implements HotProductService {

    @Resource
    private HotProductDao hotProductDao;

    @Resource
    private HotProductBannerDao hotProductBannerDao;

    @Resource
    private HotProductRecommendDao hotProductRecommendDao;

    @Resource
    private HotProductCategoryDao hotProductCategoryDao;

    @Resource
    private HotCategoryProductDao hotCategoryProductDao;

    @Override
    public CommonPage<HotProductResponse> selectList(HotProductRequest hotProductRequest) {
        PageHelper.startPage(hotProductRequest.getPageNum(), hotProductRequest.getPageSize());
        return CommonPage.restPage(hotProductDao.selectList(hotProductRequest));
    }

    @Override
    public List<HotProductChoose> hotProductChooseList(String productName) {
        return hotProductDao.hotProductChooseList(productName);
    }

    @Override
    public Integer insertHotProduct(Integer productId, String icon) {
        return hotProductDao.insertHotProduct(productId, icon);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer copyHotProduct(Integer id, Integer productId, String icon) {
        // 获取当前需要复制的爆品商品
        HotProduct hotProduct = hotProductDao.getHotProduct(id);

        // 1.复制爆品商品
        HotProduct copy = new HotProduct();
        copy.setProductId(productId);
        copy.setIcon(icon);
        copy.setStatus(hotProduct.getStatus());
        Integer count = hotProductDao.copyInsert(copy);
        //执行完复制后的爆品商品主键id自增策略
        Integer hotProductId = copy.getId();

        // 2.复制爆品商品-Banner列表
        List<HotProductBanner> bannerList = hotProductBannerDao.getBannerListByHpi(hotProduct.getId());
        if (!CollectionUtils.isEmpty(bannerList)) {
            for(HotProductBanner hpb : bannerList) {
                HotProductBanner banner = new HotProductBanner();
                banner.setHotProductId(hotProductId);
                banner.setProductId(hpb.getProductId());
                banner.setType(hpb.getType());
                banner.setImage(hpb.getImage());
                banner.setDescr(hpb.getDescr());
                banner.setSort(hpb.getSort());
                hotProductBannerDao.insertBanner(banner);
            }
        }

        // 3.复制爆品商品-推荐列表
        List<HotProductRecommend> recommendList = hotProductRecommendDao.getListByHpi(hotProduct.getId());
        if(!CollectionUtils.isEmpty(recommendList)) {
            for(HotProductRecommend hpr : recommendList) {
                hotProductRecommendDao.insertHotProductRecommend(hpr.getProductId(), hotProductId, hpr.getSort());
            }
        }

        // 4.复制爆品商品-分类
        List<HotProductCategory> categoryList = hotProductCategoryDao.getCategoryListByHpi(hotProduct.getId());
        if(!CollectionUtils.isEmpty(categoryList)) {
            for(HotProductCategory hpc : categoryList) {
                HotProductCategory category = new HotProductCategory();
                category.setHotProductId(hotProductId);
                category.setName(hpc.getName());
                category.setSort(hpc.getSort());
                category.setStatus(hpc.getStatus());
                hotProductCategoryDao.copyCategory(category);
                //执行完复制后的爆品分类商品主键id自增策略
                Integer hotProductCategoryId = category.getId();

                // 4.复制爆品商品-分类-商品
                List<HotCategoryProduct> categoryProductList = hotCategoryProductDao.getCategoryProductListByHpci(hpc.getId());
                if(!CollectionUtils.isEmpty(categoryProductList)) {
                    for(HotCategoryProduct hcp : categoryProductList) {
                        hotCategoryProductDao.insertHotCategoryProduct(hotProductCategoryId, hcp.getProductId(), hcp.getSort());
                    }
                }
            }
        }

        return count;
    }
}
